package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.Shop;
import com.hualan.dao.MenDianDao;
import com.hualan.dao.MenDianDaoImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/add")
public class PanghuAddServlet extends HttpServlet {
    private MenDianDao menDianDao = new MenDianDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Map<String,Object> map = new HashMap<>();
//      传递来的数据
//        String idString = req.getParameter("shopId");
//         判断id格式是否正确
//        if (idString == null || idString.length() < 1){
//            map.put("code",300);
//            map.put("mwssage","id参数不合法");
//            resp.getWriter().write("id错误");
//            resp.getWriter().write(JSON.toJSONString(map));
//            return;
//        }

//        int shopId = Integer.parseInt(idString);
        String shopName = req.getParameter("shopName");
        String shopAddress = req.getParameter("shopAddress");
        String shopCity = req.getParameter("shopCity");
        String shopDescribe = req.getParameter("shopDescribe");
        String shopDistrict = req.getParameter("shopDistrict");
        String shopImage = req.getParameter("shopImage");
        String shopLatitude = req.getParameter("shopLatitude");
        String shopLongitude = req.getParameter("shopLongitude");
        String shopPhone = req.getParameter("shopPhone");
        String shopProvince = req.getParameter("shopProvince");
        String addTime = req.getParameter("addTime");
//        Date addTime = (Date) addTime1;
//        先判断id是否存在
//        Shop shop = menDianDao.selectMenDianById(shopId);
//        if (shop != null){
//            map.put("code",301);
//            map.put("message","id已经存在");
//            resp.getWriter().write(JSON.toJSONString(map));
//            return;
//        }
        //          说明id不存在 可以正常新增
        int row = menDianDao.addMenDian(new Shop(shopName,shopDescribe,shopImage,shopPhone,shopProvince,shopCity,
                shopDistrict,shopAddress,shopLongitude,shopLatitude,addTime));
        if (row < 1){
            map.put("code",302);
            map.put("message","新增失败");
        }
        else {
            map.put("code",200);
            map.put("message","新增成功");
        }
        resp.getWriter().write(JSON.toJSONString(map));

    }
}
